package com.zjkj.mp.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.springframework.stereotype.Component;

/**
 * 
 * 数据方法封装包
 * 
 * @author <a href="mailto:409384898@qq.com">陈晨</a>
 * @history
 * <TABLE id="HistoryTable" border="1">
 * 	<TR><TD>时间</TD><TD>描述</TD><TD>作者</TD></TR>
 *	<TR><TD>2012-8-19</TD><TD>创建初始版本</TD><TD>陈晨</TD></TR>
 * </TABLE>
 */

@Component
public class Toolkit {

	/**
	 * 获取32位的Uuid
	 * @return
	 */
	public String getID(){
		String uuid = UUID.randomUUID().toString();
		uuid = uuid.replace("-", "");
		return uuid;
	}
	
	/**
	 * 获取时间格式yyyyMMddHHmmss
	 * @return
	 */
	public String getSystemTime(){
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String time = sdf.format(date);
		return time;
	}
	
	/**
	 * 获取时间格式yyyy-MM-dd HH：mm：ss
	 * @return
	 */
	public String getSplitTime(String time){
		String s = time.substring(0, 4) + "-" + time.substring(4, 6) + "-" + time.substring(6, 8) + " " + time.substring(8, 10)
				+ ":" + time.substring(10, 12) + ":" + time.substring(12, 14);
		return s;
	}
	
	/**
	 * 转化为yyyyMMddHHmmss格式
	 * @return
	 */
	public String getTrimTime(String time){
		String s = time.substring(0, 4) + time.substring(5, 7) + time.substring(8, 10) + time.substring(11, 13)
				+ time.substring(14, 16) + time.substring(17, 19);
		return s;
	}
	
	/**
	 * 获取日期格式yyyyMMdd
	 * @return
	 */
	public String getSystemDate(){
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String s = sdf.format(date);
		return s;
	}
	
	/**
	 * 获取日期格式yyyy-MM-dd
	 * @return
	 */
	public String getSplitDate(String date){
		String s = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8);
		return s;
	}
	
	/**
	 * 转化为yyyyMMdd格式
	 * @param date
	 * @return
	 */
	public String getTrimDate(String date){
		String newDate = date.substring(0, 4) + date.substring(5, 7) + date.substring(8, 10);
		return newDate;
	}
	
	/**
	 * 分析导入文本信息，从杂乱的格式中，提取数据，变成手机号码分隔的形式展现
	 * @param str
	 * @return
	 */
	public String AnalyseInfo(String str){
		StringBuffer sb = new StringBuffer();
		String temp;
		char[] ch = str.toCharArray();
		for (int i = 0; i < str.length(); i++) {
			if(ch[i]=='1'){
				if(i+11>str.length()){
					System.out.println("位数不足会有月界操作！");
					break;
				}
				temp = str.substring(i, i+11);
				boolean flag = phoneOrNot(temp);
				if(flag){
					//flag为true是手机号码，进行移位操作
					long num = Long.parseLong(temp);
					System.out.println(num);
					i=i+11;
					sb.append(temp).append(",");
				}else{
					//手机号码分析有误
				}
			}
		}
		temp = sb.toString().substring(0, sb.length()-1);
		return temp;
	}
	
	/**
	 * 判断是否为手机号码
	 * @param str
	 * @return
	 */
	public static boolean phoneOrNot(String str){
		boolean flag = true;
		char[] ch = str.toCharArray();
		for (int i = 0; i < str.length(); i++) {
			int num = ch[i];
			if(num<48||num>57){
				flag = false;
				System.out.println("分析结果不是手机号码！");
				break;
			}
		}
		return flag;
	}
	
	/**
	 * 删除重复的号码信息
	 * @param phoneContent
	 * @return
	 */
	public String delSameNumber(String phoneContent){
		StringBuilder sb = new StringBuilder();
		String[] str = phoneContent.split(",");
		Set<String>set = new HashSet<String>();
		for (int i = 0; i < str.length; i++) {
			set.add(str[i]);
		}
		Iterator<String> lter = set.iterator();
		while(lter.hasNext()){
			sb.append(lter.next()).append(",");
		}
		String retStr = sb.toString();
		if(retStr.length()==0){
			return retStr;
		}else{
			return retStr.substring(0, retStr.length()-1);
		}
	}
	
	/**
	 * 短信号码分类：
	 * 移动号段：134~139，147，150~152，157~159，181~183，187~188
	 * 联通号段：130~132，155~156，185~186
	 * 电信号段：133，153，180，189
	 * @param str
	 * @return
	 */
	public Map<String, Object> messageList(String str){
		Map<String, Object> map = new HashMap<String, Object>();
		String[] number = str.split(",");
		StringBuilder sb_yd = new StringBuilder();
		StringBuilder sb_lt = new StringBuilder();
		StringBuilder sb_dx = new StringBuilder();
		for (int i = 0; i < number.length; i++) {
			if(number[i]==null||"".equals(number[i])){
				break;
			}else{
				String temp = number[i].substring(0, 3);
				int temp_i = Integer.parseInt(temp);
				if((133<temp_i&&temp_i<140)||temp_i==147||(temp_i>149&&temp_i<153)
						||(temp_i>156&&temp_i<160)||(temp_i>180&&temp_i<184)||(temp_i>186&&temp_i<189)){
					sb_yd.append(number[i]).append(",");
				}
				if((129<temp_i&&temp_i<133)||(temp_i>154&&temp_i<157)||(temp_i>184&&temp_i<187)){
					sb_lt.append(number[i]).append(",");
				}
				if(133==temp_i||temp_i==153||temp_i==180||temp_i==189){
					sb_dx.append(number[i]).append(",");
				}
			}
		}
		if(sb_yd.length()!=0){
			String yd = sb_yd.toString().substring(0, sb_yd.length()-1);
			map.put("yd", yd);
		}
		if(sb_lt.length()!=0){
			String lt = sb_lt.toString().substring(0, sb_lt.length()-1);
			map.put("liantong", lt);
		}
		if(sb_dx.length()!=0){
			String dx = sb_dx.toString().substring(0, sb_dx.length()-1);
			map.put("dx", dx);
		}
		return map;
	}
	
}
